package demo_0427.homework1;

import demo_0416.hw.Account;

import java.io.*;
import java.util.*;

public class T4_0427 {
    /*4.使用List<Map<String,Object>>类型存储学生信息，并在其上进行增删改查操作。
    （类似与银行账目，可以显示一个菜单操作增删改查功能）*/
    private static final String dataFilePath = "resource/studentmassage";
    private static final String printFilePath = "resource/studentprint";
    public static void main(String[] args) {
        List<Map<String,Object>> list = loadStudents();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            menu();
            System.out.println("请输入你的选择：");
            int choice = scanner.nextInt();
            scanner.nextLine();
            switch (choice) {
                case 1:
                    addStudent(list,scanner);
                    break;
                case 2:
                    System.out.println("请输入要删除的学生学号：");
                    int id = scanner.nextInt();
                    scanner.nextLine();
                    deleteStudent(list,id);
                    break;
                case 3:
                    System.out.println("请输入要修改的学生学号：");
                    id = scanner.nextInt();
                    scanner.nextLine();
                    updateStudent(list,id,scanner);
                    break;
                case 4:
                    System.out.println("请输入要查询的学生学号：");
                    id = scanner.nextInt();
                    scanner.nextLine();
                    searchStudent(list,id);
                    break;
                case 5:
                    System.out.println(list);
                    break;
                case 6:
                    printStudents(list);
                    break;
                case 7:
                    System.out.println("退出程序！");
                    saveStudents(list);
                    return;

                default:
                    System.out.println("输入有误，请重新输入！");
                    break;
            }

        }
    }
    private static List<Map<String,Object>> loadStudents() {
        List<Map<String,Object>> list = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(dataFilePath))) {
            br.readLine();
            String line;
            while ((line = br.readLine()) != null) {
                String[] parts = line.split("\\s+");
                String name = parts[0];
                int age = Integer.parseInt(parts[1]);
                String gender = parts[2];
                int id = Integer.parseInt(parts[3]);
                String hometown = parts[4];
                Map<String,Object> student = new HashMap<>();
                student.put("姓名",name);
                student.put("年龄",age);
                student.put("性别",gender);
                student.put("学号",id);
                student.put("籍贯",hometown);
                list.add(student);
            }
            return list;
        }   catch (IOException e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    public static void saveStudents( List<Map<String,Object>> list) {
        try (PrintWriter pw = new PrintWriter(new FileWriter(dataFilePath))) {
            pw.println("姓名\t年龄\t性别\t学号\t籍贯");
            for (Map<String,Object> student : list) {
                pw.printf("%s\t%d\t%s\t%d\t%s\n", student.get("姓名"), student.get("年龄"), student.get("性别"), student.get("学号"), student.get("籍贯"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void printStudents(List<Map<String, Object>> list) {
        try (PrintWriter pw = new PrintWriter(new FileWriter(printFilePath))) {
            for (Map<String, Object> student : list) {
                pw.println(student);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    private static void searchStudent(List<Map<String, Object>> list, int id) {
        for(Map<String,Object> student : list) {
            if (student.get("学号").equals(id)) {
                System.out.println(student);
                return;
            }
        }
        System.out.println("未找到该学生！");
    }

    private static void updateStudent(List<Map<String, Object>> list, int id, Scanner scanner) {
        for(Map<String,Object> student : list) {
            if (student.get("学号").equals(id)) {
                System.out.println("请输入学生姓名：");
                String name = scanner.nextLine();
                System.out.println("请输入学生年龄：");
                int age = scanner.nextInt();
                scanner.nextLine();
                System.out.println("请输入学生性别：");
                String gender = scanner.nextLine();
                System.out.println("请输入学生籍贯：");
                String hometown = scanner.nextLine();
                student.put("姓名",name);
                student.put("年龄",age);
                student.put("性别",gender);
                student.put("籍贯",hometown);
                System.out.println("修改成功！");
                return;
            }
        }
        System.out.println("未找到该学生！");
    }

    private static void deleteStudent(List<Map<String, Object>> list, int id) {
        for(Map<String,Object> student : list) {
            if (student.get("学号").equals(id)) {
                list.remove(student);
                System.out.println("删除成功！");
                return;
            }
        }
        System.out.println("未找到该学生！");
    }

    private static void addStudent(List<Map<String, Object>> list, Scanner scanner) {
        System.out.println("请输入学生姓名：");
        String name = scanner.nextLine();
        System.out.println("请输入学生年龄：");
        int age = scanner.nextInt();
        scanner.nextLine();
        System.out.println("请输入学生性别：");
        String gender = scanner.nextLine();
        System.out.println("请输入学生学号：");
        int id = scanner.nextInt();
        scanner.nextLine();
        System.out.println("请输入学生籍贯：");
        String hometown = scanner.nextLine();
        Map<String,Object> student = new HashMap<>();
        student.put("姓名",name);
        student.put("年龄",age);
        student.put("性别",gender);
        student.put("学号",id);
        student.put("籍贯",hometown);
        list.add(student);
        System.out.println("添加成功！");
    }

    private static void menu() {
        System.out.println("------操作菜单------");
        System.out.println("请选择操作：");
        System.out.println("1.添加学生");
        System.out.println("2.删除学生");
        System.out.println("3.修改学生");
        System.out.println("4.查询学生");
        System.out.println("5.列出所有学生");
        System.out.println("6.导出学生信息");
        System.out.println("7.退出");
    }

}
